﻿using System;
using System.Web;
using System.Linq;
using System.Configuration;
using System.Security.Principal;
using System.Collections.Generic;

using Tencent.OA.Common.Organization;
using Tencent.OA.Framework.RightWay.Entity;

namespace Tencent.OA.ACM.App
{
    public class BancheStaff : Staff, IIdentity
    {
        private static string[] m_role_defini;
        //private static string _systemName;
        public static string[] GetDefinitionRoles()
        {
            if (m_role_defini == null)
                m_role_defini = ConfigurationManager.AppSettings["Roles"].Split(';');
            return m_role_defini;
        }

        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="loginName">登录名</param>
        public BancheStaff(string loginName)
            : base(loginName)
        {
            Roles = new List<Role>();
            OperationList = new List<Operation>();
        }


        /// <summary>
        /// 获取当前登陆用户的信息
        /// </summary>
        /// <returns></returns>
        public static BancheStaff GetCurrentStaff()
        {
            HttpContext context = HttpContext.Current;
            BancheStaff staff = context.Session[SecurityModule.SessionKey] as BancheStaff;
            return staff;
        }

       
        /// <summary>
        /// 当前登录人部门ID
        /// </summary>
        /// <returns></returns>
        public static int CurrentStaffDepID
        {
            get
            {
                BancheStaff s = BancheStaff.GetCurrentStaff();
                if (s != null)
                    return s.DepartmentId;
                return 0;
            }
        }
        /// <summary>
        /// 取当前系统的名称
        /// </summary>
        public static string CurrentStaffSystemName
        {
            get
            {
                Staff staff = GetCurrentStaff();
                Dept dept = new Dept((new Dept(staff.DepartmentId).ParentId));
                return dept.Name;
            }

        }
        /// <summary>
        /// 当前登录人部门名称
        /// </summary>
        /// <returns></returns>
        public static string CurrentStaffDepName
        {
            get
            {
                BancheStaff s = BancheStaff.GetCurrentStaff();
                if (s != null)
                    return s.DepartmentName;
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取当前用户的ID
        /// </summary>
        /// <returns></returns>
        public static int CurrentStaffID
        {
            get
            {
                BancheStaff s = BancheStaff.GetCurrentStaff();
                if (s != null)
                    return s.Id;
                return 0;
            }
        }
        /// <summary>
        /// 获取当前用户中文名称
        /// </summary>
        public static string CurrentStaffChineseName
        {
            get
            {
                BancheStaff s = BancheStaff.GetCurrentStaff();
                if (s == null)
                    return string.Empty;
                return s.ChineseName;
            }
        }
        /// <summary>
        /// 获取当前用户的全名
        /// </summary>
        /// <returns></returns>
        public static string CurrentStaffFullName
        {
            get
            {
                BancheStaff s = BancheStaff.GetCurrentStaff();
                if (s != null)
                    return s.FullName;
                return "";
            }
        }

        /// <summary>
        /// 是否是管理员
        /// </summary>
        public static bool IsSystemAdmin
        {
            get
            {
                BancheStaff s = BancheStaff.GetCurrentStaff();
                return s.ContainsOperateCode("SystemAdmin");

            }
        }
        /// <summary>
        /// 获取当前用户的所在部门
        /// </summary>
        /// <returns></returns>
        public static string CurrentStaffDepartmentName
        {
            get
            {
                BancheStaff s = BancheStaff.GetCurrentStaff();
                if (s != null)
                    return s.DepartmentName;
                return "";
            }
        }

        /// <summary>
        /// 授权类型
        /// </summary>
        public string AuthenticationType
        {
            get { return "Passport"; }
        }

        /// <summary>
        /// 是否已授权
        /// </summary>
        public bool IsAuthenticated { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name
        {
            get { return LoginName; }
        }

        /// <summary>
        /// 用户有权限的操作。
        /// </summary>
        public List<Operation> OperationList { get; set; }

        /// <summary>
        /// 用户所属角色。
        /// </summary>
        public List<Role> Roles { get; set; }



        /// <summary>
        /// 查找当前用户的角色组中是否包含指定的角色代码。
        /// </summary>
        /// <param name="roleCode"></param>
        /// <returns></returns>
        public bool ContainsRole(string roleCode)
        {
            var role = Roles.FirstOrDefault(r => r.Code == roleCode);
            if (role != null)
                return true;
            return false;
        }
        /// <summary>
        /// 查找当前用户是否具有指定的权限
        /// </summary>
        /// <param name="operateCode"></param>
        /// <returns></returns>
        public bool ContainsOperateCode(string operateCode)
        {
            var operate = OperationList.FirstOrDefault(r => r.Code.Equals(operateCode, StringComparison.CurrentCultureIgnoreCase));
            if (operate != null)
                return true;
            return false;
        }

        #region IIdentity 成员

        string IIdentity.AuthenticationType
        {
            get { return this.AuthenticationType; }
        }

        bool IIdentity.IsAuthenticated
        {
            get { return this.IsAuthenticated; }
        }

        string IIdentity.Name
        {
            get { return this.Name; }
        }

        #endregion
    }
}
